package com.books.service.page;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 饿熊的咆哮
 * @Date 2021/9/26 21:56
 * @Version 1.0
 */
@Service
public class PageService {
    /**
     * 返回指定页的list
     * @param list
     * @param pageNum
     * @param nowNum
     * @return
     */
    public <T> List<T> findList(List<T> list, Integer pageNum, Integer nowNum){
        int first = (nowNum-1)*pageNum;
        List<T> pageList = new ArrayList<T>();
        for (int i = first; i < first+pageNum; i++) {
            if (i==list.size()){
                break;
            }
            pageList.add(list.get(i));
        }
        return pageList;
    }

    /**
     * 返回总共多少页
     * @param list
     * @param pageNum
     * @return
     */
    public <T> Integer findAllPage(List<T> list, Integer pageNum){
        if (list.size()%pageNum==0){
            return list.size()/pageNum;
        }else {
            return list.size()/pageNum+1;
        }
    }
}
